﻿package com.tomvian.maths
{
	import flash.display.Graphics;
	import com.tomvian.maths.Vector2D;
	
	/**
	 * Line
	 * @author Thomas Vian
	 */
	public class Line 
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public var start:Vector2D;
		public var end:Vector2D;
		
		private var _diff:Vector2D;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		public function get length():Number 
		{ 
			_diff.x = end.x - start.x;
			_diff.y = end.y - start.y;
			return _diff.length; 
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Line(start:Vector2D, end:Vector2D)
		{
			_start = start;
			_end = end;
			_diff = new Vector2D();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function getPoint(t:Number, vector2D:Vector2D = null):Vector2D
		{
			if(!vector2D) vector2D = new Vector2D();
			vector2D.copy(start);
			return vector2D.interpolateToMod(end, t);
		}
		
		public function getTangentAngle(t:Number = 0.0):Number
		{
			_diff.x = end.x - start.x;
			_diff.y = end.y - start.y;
			return _diff.angle;
		}
		
		public function getTangentVector2D(t:Number = 0.0):Vector2D
		{
			_diff.x = end.x - start.x;
			_diff.y = end.y - start.y;
			return _diff.normalised();
		}
		
		public function getNormal(t:Number = 0.0):Vector2D
		{
			_diff.x = end.x - start.x;
			_diff.y = end.y - start.y;
			return _diff.normal();
		}
		
		public function drawTo(graphics:Graphics, colour:int = 0, thickness:Number = 1.0):void
		{
			graphics.lineStyle(thickness, colour);
			graphics.moveTo(start.x, start.y);
			graphics.lineTo(end.x, end.y);
		}
	}
}